package my.syncfilter;

import my.syncfilter.entity.FileEntity;
import my.syncfilter.entity.SumFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

public class ZipFileProcessor extends AbstractFileProcessor {

    private static Logger logger = LoggerFactory.getLogger(ZipFileProcessor.class);

    public ZipFileProcessor(String prefix, FileProcessor nextDataTransfer) {
        super(prefix, nextDataTransfer);
    }

    @Override
    public boolean process(FileEntity data) {
        SumFile sumFile = data.getSumFile();
        if (sumFile == null || sumFile.getFileName() == null) {
            return true;
        }
        List<String> ls = new ArrayList<>();
        //压缩文件
        ls.add(sumFile.getFileName());
        String zipName = sumFile.getFileName().substring(0, sumFile.getFileName().length() - 4) + ".zip";
        try (FileOutputStream fou = new FileOutputStream(sumFile.getBasePath() + zipName)) {
            ZipUtil.generateZip(fou, billConfig.getFileBasePath(), ls);
            SumFile zip = new SumFile();
            zip.setFileName(zipName);
            zip.setBasePath(billConfig.getFileBasePath());
            data.setZip(zip);
        } catch (Exception e) {
            logger.error("压缩文件异常", e);
        }
        return true;
    }

}